package com.spring.gop.proj.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.spring.gop.proj.interfaces.NoticeDao;
import com.spring.gop.proj.interfaces.NoticeService;
import com.spring.gop.proj.model.Notice;

@Service
public class NoticeServiceImpl implements NoticeService {

	@Autowired NoticeDao noticeDao;
	
	public List<Notice> selectNotices() {
		return noticeDao.selectNotices();
	}
	
	public Notice selectNotice(int num) {
		Notice notice = noticeDao.selectNotice(num);
		String contentTxt = notice.getContent().replaceAll("\r\n", "<br/>");
		notice.setContent(contentTxt);
		return notice;
	}
	
	@Transactional
	public void insertNotice(Notice notice) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		notice.setRegdate(sdf.format(cal.getTime()));
		noticeDao.insertNotice(notice);
	}
	
	public Notice selectLastNotice() {
		return noticeDao.selectLastNotice();
	}
}
